package filters;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameFilter implements FileFilter {
	
	private String _joker;
	private Pattern _pat;
	private boolean _hasNot;
	
	public NameFilter (String joker, boolean hasNot){
		_joker = joker;
		_hasNot = hasNot;
		buildFilter();
		_pat = Pattern.compile(_joker);
	}
	
	private void buildFilter(){
		_joker = _joker.replaceAll("\\.", "[.]");
		_joker = _joker.replaceAll("\\*", ".*");
	}
	
	public boolean accept(File file){
		Matcher mat = _pat.matcher(file.getName());
		boolean result = false;
		if (mat.find())
			result = true;
		return (_hasNot) ? !result : result;
	}

}
